#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    vector<int> solveQueries(vector<int> &nums, vector<int> &queries)
    {
        int n = nums.size();
        unordered_map<int, int> m; // 上一个的位置
        vector<pair<int, int>> n_idxs(
            n, {-1,n}); // idx的最近idx
        auto update = [&](int i)
        {
            int num = nums[i];
            if (m.count(num))
            {
                int lstIdx = m[num];
                if(lstIdx == i) return;
                int mx = max(lstIdx, i), mi = min(lstIdx, i);
                int dis = min(mx - mi, n + mi - mx);
                // 更新自己
                if (dis < n_idxs[i].second)
                {
                    n_idxs[i] = {lstIdx, dis};
                }
                // 更新上一个
                if (dis < n_idxs[lstIdx].second)
                {
                    n_idxs[lstIdx] = {i, dis};
                }
            }
            else
            {
                n_idxs[i].first = -1;
            }
            m[num] = i;
        };
        for(int i = 0; i < n; ++i){
            update(i);
        }
        for(int i = 0; i < n; ++i){
            update(i);
        }
        for(auto &[i,d]:n_idxs){
            cout << i << " " << d << endl;
        }
        vector<int> ans;
        for(auto q:queries){
            ans.push_back(n_idxs[q].first);
        }
        cout << endl;
        return ans;
    }
};